﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="AbstractUsing", Namespace="http://kis.x-road.eu/", Target=typeof(AbstractUsing))]
    public class AbstractUsingSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected AbstractUsing clazz;
        
        public AbstractUsingSerializer(AbstractUsing clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("AbstractElem");
            if (clazz.AbstractElem!=null) {
                global::Xtee.Core.SerializationMetadata metaAbstractElem = this.GetMetaData(clazz.AbstractElem);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Abstract), clazz.AbstractElem.GetType())) {
                    Writer.WriteTypeAttribute(metaAbstractElem.TypeQualifiedName.Namespace, metaAbstractElem.TypeQualifiedName.Name);
                }
                var abstractElemSerializer = metaAbstractElem.NewSerializer(clazz.AbstractElem);
                abstractElemSerializer.WriteXmlAttributes(Writer);
                abstractElemSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : AbstractElem");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("AbstractElem2");
            if (clazz.AbstractElem2!=null) {
                if (((clazz.AbstractElem2 == null) 
                            || (clazz.AbstractElem2.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.AbstractElem2 != null) 
                            && (x < clazz.AbstractElem2.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.AbstractElem2[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaAbstractElem2 = this.GetMetaData(clazz.AbstractElem2[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Abstract), clazz.AbstractElem2[x].GetType())) {
                            Writer.WriteTypeAttribute(metaAbstractElem2.TypeQualifiedName.Namespace, metaAbstractElem2.TypeQualifiedName.Name);
                        }
                        var abstractElem2Serializer = metaAbstractElem2.NewSerializer(clazz.AbstractElem2[x]);
                        abstractElem2Serializer.WriteXmlAttributes(Writer);
                        abstractElem2Serializer.WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : AbstractElem2");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="AbstractElem") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>AbstractUsing>AbstractElem");
                }
                global::Xtee.Core.SerializationMetadata metaAbstractElem = this.GetMetaData<global::Kis.Xroad.Eu.Abstract>(Reader, "Abstract", "http://kis.x-road.eu/");
                clazz.AbstractElem = metaAbstractElem.NewEntity<global::Kis.Xroad.Eu.Abstract>();
                if ((Reader.IsEmptyElement == false)) {
                    var abstractElemSerializer = metaAbstractElem.NewSerializer(clazz.AbstractElem);
                    abstractElemSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("AbstractElem");
                    abstractElemSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>AbstractUsing>AbstractElem", Reader.LocalName);
            }
            if (Reader.LocalName=="AbstractElem2") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>AbstractUsing>AbstractElem2");
                }
                clazz.AbstractElem2 = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Abstract>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("AbstractElem2");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaAbstractElem2 = this.GetMetaData<global::Kis.Xroad.Eu.Abstract>(Reader, "Abstract", "http://kis.x-road.eu/");
                                global::Kis.Xroad.Eu.Abstract item = metaAbstractElem2.NewEntity<global::Kis.Xroad.Eu.Abstract>();
                                clazz.AbstractElem2.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    var abstractElem2Serializer = metaAbstractElem2.NewSerializer(item);
                                    abstractElem2Serializer.ReadXmlAttributes(Reader);
                                    Reader.ReadStartElement("item");
                                    abstractElem2Serializer.ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.AbstractElem2.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>AbstractUsing>AbstractElem2>item", Reader.LocalName);
                        }
                    }
                    if ((clazz.AbstractElem2.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>AbstractUsing>AbstractElem2", Reader.LocalName);
            }
        }
    }
}
